/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 *
 *
 */
package rad.framework.seam;

import rad.framework.seam.ComboBox;
import rad.framework.seam.StatusMachine;
import rad.framework.seam.AbstractEditorBean;
import rad.framework.seam.Action;
import javax.ejb.Stateful;

import rad.framework.richfaces.Picker;
import rad.framework.richfaces.RootTreeNode;
import rad.framework.seam.SampleEntity.Type;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;

@Name("sampleEntityEditor")
@Stateful
public class SampleEntityEditorBean extends AbstractEditorBean<SampleEntity>
		implements SampleEntityEditor {
	private static final long serialVersionUID = 1L;

	public RootTreeNode initSampleEntityTree() {
		return initRoot(new SampleEntityTreeNode(null, getInstance()));
	}

	@Factory(value="sampleEntity", scope=ScopeType.PAGE)
	public SampleEntity initSampleEntity() {
		return getInstance(); 
	}
	
	@Override
	protected SampleEntity createInstance() {
		SampleEntity result = new SampleEntity();
		return result;
	}

	public boolean isSimpleEntity() {
		return true;
	}
	
	@Out
	private ComboBox refEntityComboBox = new RefEntityComboBox<SampleEntityEditor, SampleEntity>(this, "reference") {
//	}.addRestriction("t1 not in #{empty sampleEntity.children ? ('aname') : sampleEntity.children.{m|m.name}}");
	}.addRestriction("t1.name != #{sampleEntity.class.name}");
	
	@Out
	private Picker childrenPicker = new RefEntityPicker<SampleEntityEditor, SampleEntity>(this, "children") {
		
		protected void add(SampleEntity instance, RefEntity child) {
			instance.addChild(child);
		}
		protected void remove(SampleEntity instance, RefEntity child) {
			instance.getChildren().remove(child);
		}
	};
	
	protected void initStatusMachine(StatusMachine statusMachine) {
		statusMachine.addStatusAction(Type.A, "submit");

		statusMachine.addStatusAction(Type.B, "submit");
		statusMachine.addStatusAction(Type.B, "something");
	}

	protected Object getCurrentStatus() {
		if (!isManaged()) {
			return "NOT_MANAGED";
		}
		return getInstance().getType();
	}

	@Action
	public String submit() {
		flush();
		return getFindOutcome();
	}

	@Action
	public String something() {
		flush();
		return getFindOutcome();
	}
}
